import {useMyRouter} from "../store/moduls/usemyroute";
import type {RouteLocationNormalized,NavigationGuardNext} from "vue-router"
import type {Router} from "vue-router"
import {useMneu} from "../store/moduls/menu";
import {nanoid} from 'nanoid'

export const createRouterGuards = (router:Router): void => {
    // 前置路由守卫
    router.beforeEach((to:RouteLocationNormalized, from:RouteLocationNormalized, next:NavigationGuardNext) => {
        // 开启loading
        const Loading:any = window['$loading' as any] || null;
        console.log("开启顶部loading")
        Loading && Loading.start();
        // 判断路由是否需要缓存
        // if (to.meta["cache"]) {
        //     // 用的时候创建实例，如果再函数顶上创建，会导致pinia还没有创建完毕，导致出错
        //     const myrouter = useMyRouter()
        //     myrouter.huancun(to)
        // }
        // 不用判断，全部缓存
        // 用的时候创建实例，如果再函数顶上创建，会导致pinia还没有创建完毕，导致出错
        const myrouter = useMyRouter()
        myrouter.huancun(to)
        const mymenu = useMneu()
        mymenu.routeChange(to.name as string)
        // 执行跳转
        next()
        // 结束loading
        Loading && Loading.finish();
        console.log("关闭顶部loading")
    })
}
